// pages/myorder/myorder.js
Page({ 

  /**
   * 页面的初始数据
   */
  data: {
    
  },
  delete(e){
    var that=this;
    const deid = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '您确定要删除此贴吗',
      success:function(res){
        if (res.confirm){
          const db = wx.cloud.database().collection("List");
          db.doc(deid).remove({
            success: function(res) {
              that.onShow();
                wx.showToast({
                title: '删除成功',
                icon:"none",
              })
            }
          })
        }
        else{
          wx.showToast({
            title: '已取消',
            icon:"none"
          })  
        }
      }
    })

  },
  checkboxChange(e){
    //console.log(e);
    var that = this; 
    const Cid = e.currentTarget.dataset.id;
    const check = e.detail.value;
    //console.log(Cid,check);
    const db = wx.cloud.database().collection("List");
    var app = getApp();
    db.doc(Cid).update({
      data: {
        isok: check,
      },
      success: function(res) {
        //console.log(res);
        wx.showToast({
          title: '设置成功',
        })
      }
    })
  },
  onShow() {
    var app = getApp();
    if(!app.globalData.hasLogin)
    {
      setTimeout(
        function () { //注意function这里不能缺少
          wx.switchTab({
            url: '../my/my',
          })
        }, 1500)
      wx.showToast({
        title: '请登录后再查看',
        icon:"error",
      })
      return;
      }
    wx.showLoading({
      title: '加载中',
    })
    var app = getApp();
    const id = app.globalData.useropenid;
    var that = this; 
    const db = wx.cloud.database().collection("List");
    db.where({_openid:id}).orderBy('data','desc').limit(10).get({
      success: function(res) {
        // res.data 是包含以上定义的两条记录的数组
        const templist = res.data;
        //console.log(res);
        that.setData(
          {
            postList:templist,
          }
        );
        wx.hideLoading({
          success: (res) => {},
        })
      } 
    })
  },
  
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})